{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.字典的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小明', 'age': 18, 'gender': True, 'height': 1.75}\n"
     ]
    }
   ],
   "source": [
    "# dictionary是除列表以外python之中最灵活的数据类型\n",
    "# 字典同样可以用来存储多个数据\n",
    "#    - 通常用于存储描述一个物体的相关信息\n",
    "# 和列表的区别\n",
    "#    - 列表是有序的对象集合\n",
    "#    - 字典是无序的对象集合\n",
    "# 字典用{}定义\n",
    "# 字典使用键值对存储数据，键值对之间使用，分隔\n",
    "#    - 键key是索引\n",
    "#    - 值value是数据\n",
    "#    - 键和值之间使用：分隔\n",
    "#    - 键必须是唯一的\n",
    "#    - 值可以取任何数据类型，但键只能使用字符串、数字、元组\n",
    "\n",
    "xiaoming ={\"name\":\"小明\",\n",
    "           \"age\":18,\n",
    "           \"gender\":True,\n",
    "           \"height\":1.75}\n",
    "print(xiaoming)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{('name', 'age'): ('小明', 18)}\n"
     ]
    }
   ],
   "source": [
    "test = {(\"name\",\"age\"):(\"小明\",18)}\n",
    "print(test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.字典的常用操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.1 定义"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "xiaoming_dict = {\"name\":\"小明\"}"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.2 取值"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明\n"
     ]
    }
   ],
   "source": [
    "print(xiaoming_dict[\"name\"])\n",
    "# 在取值的时候，如果指定的key不存在，程序会报错"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "'小明'"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xiaoming_dict.get('name')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3 修改"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小小明', 'age': 18}\n"
     ]
    }
   ],
   "source": [
    "# 如果key不存在，会新增键值对\n",
    "xiaoming_dict[\"age\"]=18\n",
    "# 如果key存在，会修改已经存在的键值对\n",
    "xiaoming_dict[\"name\"]=\"小小明\"\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.4 新增"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小小明', 'age': 23}\n"
     ]
    }
   ],
   "source": [
    "# 如果key存在，修改数据\n",
    "# 如果key不存在，新增键值对\n",
    "xiaoming_dict[\"age\"]= 23\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小小明', 'age': 23}\n"
     ]
    }
   ],
   "source": [
    "# 如果key存在，不会修改数据\n",
    "# 如果key不存在，新增键值对\n",
    "xiaoming_dict.setdefault(\"name\",\"小明\")\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小小明', 'age': 23, 'address': '四川'}\n"
     ]
    }
   ],
   "source": [
    "# 将字典2的数据合并到字典1\n",
    "dict2 = {\"address\":\"四川\"}\n",
    "xiaoming_dict.update(dict2)\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.5 删除"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小小明', 'age': 23}\n"
     ]
    }
   ],
   "source": [
    "# 删除指定键值对，key不存在会报错\n",
    "del xiaoming_dict[\"address\"]\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小小明'}\n"
     ]
    }
   ],
   "source": [
    "# 删除指定键值对，key不存在会报错\n",
    "xiaoming_dict.pop(\"age\")\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Input \u001B[1;32mIn [23]\u001B[0m, in \u001B[0;36m<cell line: 1>\u001B[1;34m()\u001B[0m\n\u001B[1;32m----> 1\u001B[0m test \u001B[38;5;241m=\u001B[39m \u001B[43mxiaoming_dict\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mpop\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28mprint\u001B[39m(test)\n",
      "\u001B[1;31mTypeError\u001B[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "test = xiaoming_dict.pop()\n",
    "print(test)\n",
    "# error"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 随机删除一个键值对\n",
    "xiaoming_dict.popitem()\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 清空字典\n",
    "xiaoming_dict={\"name\":\"小明\",\"age\":16,\"address\":\"四川\"}\n",
    "xiaoming_dict.clear()\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.6 统计键值对数量"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "xiaoming_dict = {\"name\":\"小明\",\n",
    "                 \"age\":18}"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(len(xiaoming_dict))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.7 合并字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "temp_dict = {\"height\":1.75,\n",
    "             \"age\":20}\n",
    "# 注意：如果被合并的字典中包含已经存在的键值对，会覆盖原有的键值对\n",
    "xiaoming_dict.update(temp_dict)\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.8 清空字典"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "xiaoming_dict.clear()\n",
    "print(xiaoming_dict)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "xiaoming_dict = {\"name\":\"小明\",\n",
    "                 \"qq\":\"123456\",\n",
    "                 \"phone\":\"1008611\"}"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.9 遍历字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name--小明\n",
      "qq--123456\n",
      "phone--1008611\n"
     ]
    }
   ],
   "source": [
    "# 变量k是每一次循环中，获取到的键值对的key\n",
    "for i in xiaoming_dict:\n",
    "    print(\"%s--%s\"%(i,xiaoming_dict[i]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '张三', 'qq': '12345', 'phone': '110'}\n",
      "{'name': '李四', 'qq': '54321', 'phone': '1008611'}\n"
     ]
    }
   ],
   "source": [
    "card_list = [\n",
    "    {\"name\":\"张三\",\n",
    "     \"qq\":\"12345\",\n",
    "     \"phone\":\"110\"},\n",
    "    {\"name\":\"李四\",\n",
    "     \"qq\":\"54321\",\n",
    "     \"phone\":\"1008611\"}\n",
    "]\n",
    "\n",
    "# 使用多个键值对，存储描述一个物体的相关信息--描述更复杂的信息\n",
    "# 将多个字典放在一个列表中，在进行遍历\n",
    "\n",
    "for card_info in card_list:\n",
    "    print(card_info)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.应用场景"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 将多个字典放在一个列表中，在进行遍历，在循环体内针对每一个字典进行相同的处理"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}